package com.yf1706.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yf1706.service.SalaryDetailService;
import com.yf1706.vo.SalaryDetailVO;

@Controller
@RequestMapping("salaryDetail")
public class SalaryDetailController extends BaseController{

	@Autowired
	private SalaryDetailService salaryDetailService;
	
	@RequestMapping("queryById")
	@ResponseBody
	public Map<String, List<Object>> queryById(int Sid){
		Map<String, List<Object>> datagrid =new HashMap<String, List<Object>>();
		List<SalaryDetailVO> SalaryDetails = salaryDetailService.querySDAll(Sid);
		Map<Integer, List<SalaryDetailVO>> SalaryDetailsByEmpId =
				SalaryDetails.stream().collect(Collectors.groupingBy(SalaryDetailVO::getEmpId));
		List<Object> list = new ArrayList<Object>();
		for (List<SalaryDetailVO> value : SalaryDetailsByEmpId.values()) { 
			Map<String, Object> map = new HashMap<String, Object>();
			map.put(0+"", value.get(0).getEmpId());
			map.put(1+"", value.get(0).getEmpName());
			map.put(2+"", value.get(0).getSalaryItem());
			map.put(3+"", value.get(0).getSalaryItemValue());
			int j=4;
			for(int i=1;i<value.size();i++) {
				map.put(j+"", value.get(i).getSalaryItem());
				j++;
				map.put(j+"", value.get(i).getSalaryItemValue());
				j++;
			}
			list.add(map);
		}
		datagrid.put("rows", list);
		return datagrid;
	}
}
